<template>
	<view>
		<view class="login">
			<view class="head">
				用户登陆
			</view>
			<view>
				<form>
					<view class="cu-form-group">
						<view class="title">账号</view>
						<input placeholder="请输入账号" @input="theAccount" v-model="account">
					</view>
					<view class="cu-form-group">
						<view class="title">密码</view>
						<input placeholder="请输入密码" password="true" @input="thePassword" v-model="password">
					</view>
				</form>
				<view style="margin-top: 20px;text-align: center;position: relative;">
					<button class="cu-btn round lg bg-yellow" @click="login()">登陆</button>
					<view class="register" style="position: absolute;right: 20px;top: 10px;" @click="toRegister()">
						注册账号
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {
				password: '',
				account: '',
				userAccount: '',
				userPassword: ''
			}
		},
		methods: {
			theAccount: function(e) {
				this.userAccount = e.target.value
			},
			thePassword: function(e) {
				this.userPassword = e.target.value
			},


			toRegister() {
				uni.navigateTo({
					url: '../register/register'
				})
			},
			login() {
				// this.account = ''
				// this.password = ''

				uni.request({
					url: 'http://172.20.10.5:8080/user/login',
					data: {
						userAccount: this.account,
						userPassword: this.password
					},
					method: 'POST',
					header: {
						'content-type': 'application/json',
					},
					success: (res) => {
						console.log(res);
						if (res.data == 1 || res.data == 2) {
							store.commit("setUserAccount", this.account)
							store.commit("setIsLogin", 1)
							store.commit("setUserStatus", res.data)
							uni.navigateTo({
								url: '../control/control'
							})
						} else {
							if (res.data == 0) {
								uni.showToast({
									title: '账号或密码错误',
									icon: 'error'
								})
							}
						}
					},
					fail: (res) => {
						console.log(res);
						uni.showToast({
							title: '账号或密码错误',
							icon: 'error'
						})
					}
				})
			}
		}
	}
</script>

<style>
	.head {
		font-size: 20px;
		font-weight: 700;
		text-align: center;
		margin: 40rpx;
	}

	.register:hover {
		color: deepskyblue;
		cursor: pointer;
	}
</style>
